//
//  PTTooBar.m
//  封装TableBar
//
//  Created by 贾旭周 on 15/12/11.
//  Copyright © 2015年 gm-iMac-iOS-03. All rights reserved.
//

#import "TPTooBar.h"
#import "TPTabBarItem.h"
#import "TPTabBarItemBtn.h"
#import "TPNavigationBarView.h"
#import "ZOHBlurView.h"

/// 底部按钮Tag 基数
#define ButtonTag 198990


@interface TPTooBar ()
{
    /// 底部选择划线
    UIImageView *lineImage_;
}

/// 底部导航按钮容器
@property (nonatomic, strong) NSMutableArray *tabBtnArray;

/// 当前选择的导航按钮
@property (nonatomic, assign) TPTabBarItemBtn *selectedBtn;

@end


@implementation TPTooBar


#pragma mark- ************************初始化********************************
/********************************初始化************************************/

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _tabBtnArray = [NSMutableArray arrayWithCapacity:0];
    }

    self.backgroundColor = [UIColor whiteColor];
    
    
//    // 创建毛玻璃view
//    ZOHBlurView *viewBlur = [[ZOHBlurView alloc] initWithFrame:CGRectMake(0, 0, screenW, 64)];
//    viewBlur.blurView.barStyle = UIBarStyleDefault;
//    viewBlur.blurView.translucent = YES;
//    [self addSubview:viewBlur];
    return self;
}


#pragma mark- ***********************Function******************************
/*****************************Function************************************/

/// 初始化完成之后 创建视图
- (void)drawNecessaryView
{
    
    self.userInteractionEnabled = YES;
//    self.backgroundColor = GMK_Color_Main_BG;
    BOOL isHasTitle = NO;
    NSInteger miniNums = 0;
    if (self.aryTitle.count >0)
    {
        isHasTitle = YES;
        miniNums = (self.aryTitle.count <= self.arySelectImages.count) ? self.aryTitle.count :self.arySelectImages.count;
    }
    else
    {
        miniNums = (self.arySelectImages.count <=self.aryUnSelectImages.count) ? self.arySelectImages.count : self.aryUnSelectImages.count;
    }


    _viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0.5)];
    _viewLine.backgroundColor = [UIColor colorWithRed:204/255.0
                                                green:204/255.0
                                                 blue:204/255.0
                                                alpha:1];
    
    [self addSubview:_viewLine];
    
    CGFloat fltBtnWIdth = self.frame.size.width/(miniNums*1.0);
    //初始化底部tabbar按钮
    for (int i=0; i<miniNums; i++)
    {
        TPTabBarItem *item = [TPTabBarItem tabBarItemWithNormalImage:[_aryUnSelectImages objectAtIndex:i]
                                                    highlightedImage:[_arySelectImages objectAtIndex:i]];
        item.title = isHasTitle ? [_aryTitle objectAtIndex:i] :nil;

        TPTabBarItemBtn *button = [[TPTabBarItemBtn alloc]
                                   initWithFrame:CGRectMake(fltBtnWIdth*i, i == 1 ? -9: 0, fltBtnWIdth,i == 1 ? 58:self.frame.size.height)
                                   withItem:item];
        button.backgroundColor =[UIColor clearColor];
        [self.tabBtnArray addObject:button];
        
        if (i== 0)
        {
            button.tag = i+ButtonTag;
        }
        else if (i==1)
        {
            button.tag = i+ButtonTag + 1;
        }
        else if (i == 2)
        {
            button.tag = i+ButtonTag - 1;
        }
        
        [button addTarget:self
                   action:@selector(buttonClickAction:)
         forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];

        if (i==0 && _isHasFootLine)
        {
            lineImage_ = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width/miniNums, 2)];
            lineImage_.backgroundColor = [UIColor redColor];
            lineImage_.center = CGPointMake(fltBtnWIdth/2.0, self.frame.size.height-1);
            [self addSubview:lineImage_];
        }
    }
}

/// 手动设置导航跳转
- (void)setBottomBtnClicked:(BottomIndex)bottomIndex
{
    TPTabBarItemBtn *button = (TPTabBarItemBtn *)[self viewWithTag:bottomIndex+ButtonTag];
    [self buttonClickAction:button];
}


/// 按钮点击事件
- (void)buttonClickAction:(TPTabBarItemBtn *)clickButton
{
    /// 中间按钮tag为2＋ButtonTag
    lineImage_.center = CGPointMake(clickButton.center.x, lineImage_.center.y);

    if ([_delegate respondsToSelector:@selector(bottomViewIndexDelegate:lastSelectedTabIndex:)] && clickButton.tag-ButtonTag != 2)
    {
        [_delegate bottomViewIndexDelegate:clickButton.tag-ButtonTag lastSelectedTabIndex:self.selectedBtn.tag-ButtonTag];
    }

    if (self.selectedBtn == clickButton)
    {
        return;
    }

    /// 判断之前所在控制器位置
    if (self.selectedBtn.tag - ButtonTag == 0 && clickButton.tag-ButtonTag == 2) /// 在首页了
    {
       [[NSNotificationCenter defaultCenter] postNotificationName:@"HomeLive" object:nil];
    }
    else if (self.selectedBtn.tag - ButtonTag == 1 && clickButton.tag-ButtonTag == 2) ///  在个人页面了
    {
         [[NSNotificationCenter defaultCenter] postNotificationName:@"MeLive" object:nil];
    }
    else
    {
        
    }
    
    if (clickButton.tag-ButtonTag != 2)
    {
        //前一个按钮设置
        self.selectedBtn.selected = NO;
        //当前点击的按钮设置
        self.selectedBtn = clickButton;
        self.selectedBtn.selected = YES;
    }
}


@end
